namespace sight {

    export class MapScene extends hjc.Scene {
        private Background: eui.Group
        private customFilter: egret.CustomFilter
        private ShaderImage: eui.Rect

        private initShader() {
            let vertexSrc = `
precision mediump float;

attribute vec2 aVertexPosition;
attribute vec2 aTextureCoord;
attribute float aColor;

uniform vec2 projectionVector;

varying vec2 vTextureCoord;
varying vec4 vColor;

const vec2 center = vec2(-1.0, 1.0);

void main(void) {
    gl_Position = vec4( (aVertexPosition / projectionVector) + center , 0.0, 1.0);
    vTextureCoord = aTextureCoord;
    vColor = vec4(aColor, aColor, aColor, aColor);
}
`
            console.log(egret.web["EgretShaderLib"]["default_vert"])
            /*
                "attribute vec2 aVertexPosition;\n" +
                "attribute vec2 aTextureCoord;\n" +
                "attribute vec2 aColor;\n" +

                "uniform vec2 projectionVector;\n" +

                "varying vec2 vTextureCoord;\n" +
                "varying vec4 vColor;\n" +

                "const vec2 center = vec2(-1.0, 1.0);\n" +

                "void main(void) {\n" +
                "   gl_Position = vec4( (aVertexPosition / projectionVector) + center , 0.0, 1.0);\n" +
                "   vTextureCoord = aTextureCoord;\n" +
                "   vColor = vec4(aColor.x, aColor.x, aColor.x, aColor.x);\n" +
                "}";
                */
            let fragmentFlash = "precision lowp float;\n" +
                "varying vec2 vTextureCoord;\n" +
                "varying vec4 vColor;\n" +
                "uniform sampler2D uSampler;\n" +

                "uniform float customUniform;\n" +

                "void main(void) {\n" +
                "vec2 uvs = vTextureCoord.xy;\n" +
                "vec4 fg = texture2D(uSampler, vTextureCoord);\n" +
                "fg.rgb += sin(customUniform + uvs.x * 2. + uvs.y * 2.) * 0.2;\n" +
                "gl_FragColor = fg * vColor;\n" +
                "}";

            let fragmentBlur = `
precision mediump float;
varying vec2 vTextureCoord;

uniform vec2 blur;
uniform sampler2D uSampler;
uniform vec2 uTextureSize;
void main()
{
    const int sampleRadius = 5;
    const int samples = sampleRadius * 2 + 1;
    vec2 blurUv = blur / uTextureSize;
    vec4 color = vec4(0, 0, 0, 0);
    vec2 uv = vec2(0.0, 0.0);
    blurUv /= float(sampleRadius);

    for (int i = -sampleRadius; i <= sampleRadius; i++) {
        uv.x = vTextureCoord.x + float(i) * blurUv.x;
        uv.y = vTextureCoord.y + float(i) * blurUv.y;
        color += texture2D(uSampler, uv);
    }

    color /= float(samples);
    gl_FragColor = color;
}
`
            let frameShadow = shader.Header + shader.Shadow + shader.Main
            /*
                "precision lowp float;\n" +
                "varying vec2 vTextureCoord;\n" +
                "varying vec4 vColor;\n" +
                "uniform sampler2D uSampler;\n" +

                "uniform float customUniform;\n" +

                "void main(void) {\n" +
                "vec2 uvs = vTextureCoord.xy;\n" +
                "vec4 fg = texture2D(uSampler, vTextureCoord);\n" +
                "fg.rgb += sin(customUniform + uvs.x * 2. + uvs.y * 2.) * 0.2;\n" +
                "gl_FragColor = fg * vColor;\n" +
                "}";
            */

            let poke13 = new egret.Bitmap(RES.getRes("poke13_png"))
            let iChannel0 = new egret.RenderTexture()
            iChannel0.drawToTexture(poke13)

            this.customFilter = new egret.CustomFilter(
                vertexSrc,  //egret.web["EgretShaderLib"]["default_vert"],
                frameShadow,
                {
                    customUniform: 0,
                    blur: { x: 0, y: 0 },
                    iMouse: { x: 500, y: 500, z: 0, w: 0 },
                    iResolution: { x: 0, y: 0 },
                    iChannel0: poke13,
                }
            );
            this.customFilter.padding = 0

            let frameCount = 0
            this.addEventListener(egret.Event.ENTER_FRAME, () => {
                frameCount++
                this.customFilter.uniforms.customUniform += 0.1;
                if (this.customFilter.uniforms.customUniform > Math.PI * 2) {
                    this.customFilter.uniforms.customUniform = 0.0;
                }

                this.customFilter.uniforms.blur.x = Math.sin(frameCount / 100) * 10
            }, this);
        }
        constructor() {
            super()
            game.on(sight.ntf.UpdateMap, this.updateMap, this)
        }

        protected displayed() {
            console.debug("显示...")
            console.debug("分辨率：" + document.documentElement.clientWidth + "," + document.documentElement.clientHeight)
            console.debug("画布：" + this.ShaderImage.width + "," + this.ShaderImage.height)
            let xScale: egret.Point = new egret.Point(
                document.documentElement.clientWidth / this.ShaderImage.width,
                document.documentElement.clientHeight / this.ShaderImage.height)
            this.initShader()

            {
                let shape = new egret.Shape();
                shape.graphics.beginFill(Color.GRAY);
                shape.graphics.drawRect(0, 0, 600, 600)
                shape.graphics.endFill()
                shape.x = 0
                shape.y = 1344-600
                console.debug("shape: " + shape.width + "," + shape.height)
                this.ShaderImage.addChild(shape)

                shape.filters = [this.customFilter]
                this.customFilter.uniforms.iResolution = { x: shape.width, y: shape.height }
            }

            //this.customFilter.uniforms.iResolution = { x: this.ShaderImage.width, y: this.ShaderImage.height }
            // this.ShaderImage.filters = [this.customFilter];
            this.ShaderImage.addEventListener(egret.TouchEvent.TOUCH_MOVE, (e: egret.TouchEvent) => {
                let org = utils.GetOrgTouch(e)
                this.customFilter.uniforms.iMouse.x = org.x
                this.customFilter.uniforms.iMouse.y = this.ShaderImage.height - org.y
                console.debug("Tap Move: " + org.x + "," + org.y)
            }, null)
            return

            this.Background.addChild(game.proxy(sight.MapProxy).Border.Draw())

            game.proxy(sight.MapProxy).Stones.forEach((p) => {
                this.Background.addChild(p.Draw())//.filters = [this.customFilter]
            })

            this.Background.touchEnabled = true
            this.Background.touchChildren = true

            this.Background.addEventListener(egret.TouchEvent.TOUCH_MOVE, (e: egret.TouchEvent) => {
                let org = utils.GetOrgTouch(e)
                game.proxy(sight.MapProxy).UpdataEyeLocation(org.x, org.y)
                console.log("TOUCH_TAP:%d,%d", org.x, org.y)
            }, null)


            this.updateMap()
        }

        private updateMap() {
            MapProxy.Instance.Lines.forEach((p) => {
                this.Background.addChild(p.Draw())
            })
            MapProxy.Instance.Points.forEach((p) => {
                this.Background.addChild(p.Draw())
            })
            MapProxy.Instance.Shadows.forEach((p) => {
                this.Background.addChild(p.Draw())
            })
            MapProxy.Instance.EyeLocaion.forEach((p) => {
                p.Draw(this.Background)
            })
        }
    }
}